package com.aierjun.mainview.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



/**
 * Created by Administrator on 2017/6/7 0007.
 */

public class MainView implements View.OnClickListener {
    private Context context;
    private int[] textPadding;
    private int[] imagePaddding;
    private int background = Color.WHITE;
    private int[] ids;
    private String[] text;
    private int[] mipmapUnCheck;
    private int[] mipmapCheck;
    private OnItemIdClickListener onClickListener;
    private View nowView;
    private LinearLayout linearLayout;
    private int position = 0;

    private boolean defaultCheckItem=true;
    private int defaultCheckNum=0;

    private int idLayout=123;

    @IdRes private int fragmentId=123456;

    @ColorRes private int textUnCheckColor=0xFF888888;
    @ColorRes private int textCheckColor=0xFF000000;


    private Fragment[] fragment;


    public MainView(FragmentActivity context) {
        this.context = context;
        return;
    }

    public View initView() {
        try {
            RelativeLayout relativeLayout = new RelativeLayout(context);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            relativeLayout.setLayoutParams(params);
            linearLayout = new LinearLayout(context);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            linearLayout.setLayoutParams(layoutParams);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setWeightSum(text.length);
            linearLayout.setBackgroundColor(ContextCompat.getColor(context, background));
            linearLayout.setId(idLayout);

            for (int i = 0; i < text.length; i++) {
                LinearLayout linearLayout1 = new LinearLayout(context);
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
                linearLayout1.setLayoutParams(layoutParams1);
                layoutParams1.weight = 1;
                linearLayout1.setOrientation(LinearLayout.VERTICAL);
                linearLayout1.setHorizontalGravity(LinearLayout.HORIZONTAL);
                ImageView imageView11 = new ImageView(context);
                imageView11.setImageDrawable(ContextCompat.getDrawable(context, mipmapUnCheck[i]));
                if (imagePaddding != null)
                    imageView11.setPadding(imagePaddding[0], imagePaddding[1], imagePaddding[2], imagePaddding[3]);
                TextView textView11 = new TextView(context);
                textView11.setText(text[i]);
                textView11.setGravity(Gravity.CENTER);
                if (textUnCheckColor != 0)
                    textView11.setTextColor(ContextCompat.getColor(context,textUnCheckColor));
                if (textPadding != null)
                    textView11.setPadding(textPadding[0], textPadding[1], textPadding[2], textPadding[3]);
                if (defaultCheckItem==true)
                    if (i==defaultCheckNum){
                        imageView11.setImageDrawable(ContextCompat.getDrawable(context, mipmapCheck[i]));
                        textView11.setTextColor(ContextCompat.getColor(context,textCheckColor));
                        nowView=linearLayout1;
                        if (fragment!=null)
                            replaceAllFragment(((FragmentActivity)context).getSupportFragmentManager().beginTransaction(),i);
                    }
                linearLayout1.addView(imageView11);
                linearLayout1.addView(textView11);
                linearLayout.addView(linearLayout1);
                try {
                    if (ids != null)
                        linearLayout1.setId(ids[i]);
                } catch (Exception e) {
                    Log.d("Ani MainView","navigationbar ids参数错误,请保持item和id个数一致");
                }
                if (onClickListener != null)
                    linearLayout1.setOnClickListener(this);
            }
            relativeLayout.addView(linearLayout);
            FrameLayout frameLayout=new FrameLayout(context);
            RelativeLayout.LayoutParams paramsF=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            paramsF.addRule(RelativeLayout.ABOVE,idLayout);
            frameLayout.setLayoutParams(paramsF);
//            frameLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.red));
            frameLayout.setId(fragmentId);
            frameLayout.setTag("123456");
            relativeLayout.addView(frameLayout);
            return relativeLayout;
        } catch (Exception e) {
            Log.d("Ani MainView","navigationbar 参数错误");
        }
        return null;
    }

    public MainView setTextPadding(int left, int top, int right, int bottom) {
        textPadding = new int[]{left, top, right, bottom};
        return this;
    }

    public MainView setImagePadding(int left, int top, int right, int bottom) {
        imagePaddding = new int[]{left, top, right, bottom};
        return this;
    }

    public MainView setItemBackgroundColor(int background) {
        this.background = background;
        return this;
    }

    public MainView setIds(int[] ids) {
        this.ids = ids;
        return this;
    }

    public MainView setItemText(String[] text) {
        this.text = text;
        return this;
    }

    public MainView setItemUnCheckMipmap(int[] mipmapUnCheck) {
        this.mipmapUnCheck = mipmapUnCheck;
        return this;
    }

    public MainView setItemCheckMipmap(int[] mipmapCheck) {
        this.mipmapCheck = mipmapCheck;
        return this;
    }

    public MainView setOnItemIdClickListener(OnItemIdClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public MainView setItemCheckTextColor(int checkTextColor) {
        this.textCheckColor = checkTextColor;
        return this;
    }

    public MainView setItemUnCheckTextColor(int unCheckTextColor) {
        this.textUnCheckColor = unCheckTextColor;
        return this;
    }

    public MainView setDefaultCheck(boolean defaultCheck){
        this.defaultCheckItem=defaultCheck;
        return this;
    }

    public MainView setDefaultCheck(boolean defaultCheck,int defaultCheckNum){
        this.defaultCheckItem=defaultCheck;
        this.defaultCheckNum=defaultCheckNum;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (nowView != null) {
            LinearLayout linearLayout = (LinearLayout) nowView;
            ImageView imageView = (ImageView) linearLayout.getChildAt(0);
            TextView textView = (TextView) linearLayout.getChildAt(1);
            if (mipmapUnCheck != null)
                imageView.setImageDrawable(ContextCompat.getDrawable(context, mipmapUnCheck[position]));
            if (textUnCheckColor != 0)
                textView.setTextColor(ContextCompat.getColor(context,textUnCheckColor));
        }
        LinearLayout linearLayout = (LinearLayout) v;
        ImageView imageView = (ImageView) linearLayout.getChildAt(0);
        TextView textView = (TextView) linearLayout.getChildAt(1);
        for (int i = 0; i < ids.length; i++) {
            if (v.getId() == ids[i]) {
                if (mipmapCheck != null)
                    imageView.setImageDrawable(ContextCompat.getDrawable(context, mipmapCheck[i]));
                if (textCheckColor != 0)
                    textView.setTextColor(ContextCompat.getColor(context, textCheckColor));
                onClickListener.onClickListenrt(i, v);
                position = i;
                this.replaceAllFragment(((FragmentActivity)context).getSupportFragmentManager().beginTransaction(),i);
            }
        }
        nowView = v;
    }

    public interface OnItemIdClickListener {
        void onClickListenrt(int position, View view);
    }

    public void setAllItemUnCheck(){
        if (nowView!=null){
            LinearLayout linearLayout = (LinearLayout) nowView;
            ImageView imageView = (ImageView) linearLayout.getChildAt(0);
            TextView textView = (TextView) linearLayout.getChildAt(1);
            if (mipmapUnCheck != null)
                imageView.setImageDrawable(ContextCompat.getDrawable(context, mipmapUnCheck[position]));
            if (textUnCheckColor != 0)
                textView.setTextColor(ContextCompat.getColor(context, textUnCheckColor));
        }
    }

    public void setItemCheck(int positions){
        setAllItemUnCheck();
        LinearLayout linearLayoutChildAt=(LinearLayout) linearLayout.getChildAt(positions);
        ImageView imageView = (ImageView) linearLayoutChildAt.getChildAt(0);
        TextView textView = (TextView) linearLayoutChildAt.getChildAt(1);
        if (mipmapCheck != null)
            imageView.setImageDrawable(ContextCompat.getDrawable(context, mipmapCheck[positions]));
        if (textCheckColor != 0)
            textView.setTextColor(ContextCompat.getColor(context, textCheckColor));
        onClickListener.onClickListenrt(positions, linearLayoutChildAt);
        position = positions;
        nowView = linearLayoutChildAt;
        replaceAllFragment(((FragmentActivity)context).getSupportFragmentManager().beginTransaction(),positions);
    }

    public MainView setFragment(Fragment[] fragment){
        this.fragment=fragment;
        return this;
    }

    //隐藏所有Fragment
    public void hideAllFragment(FragmentTransaction transaction){
        if(fragment!=null){
            for (int i=0;i<fragment.length;i++)
                if (fragment[i].isVisible())
                    transaction.hide(fragment[i]);
        }
    }

    //add所有Fragment
    public void addAllFragment(FragmentTransaction transaction,int position){
        hideAllFragment(transaction);
        if(fragment!=null){
            if (fragment[position].isAdded()){
                transaction.show(fragment[position]);
            }else{
                transaction.add(fragmentId,fragment[position],""+position);
                transaction.show(fragment[position]);
            }
            transaction.commit();
        }
    }
    //add所有Fragment
    public void replaceAllFragment(FragmentTransaction transaction, int position){
        hideAllFragment(transaction);
        try {
            if(fragment!=null) {
                if (fragment[position].isAdded()){
                    transaction.show(fragment[position]);
                }else{
                    transaction.replace(fragmentId, fragment[position]);
                    transaction.show(fragment[position]);
                }
                transaction.commit();
            }
        }catch (Exception e){
            if(fragment!=null){
                if (fragment[position].isAdded()){
                    transaction.show(fragment[position]);
                }else{
                    transaction.add(fragmentId,fragment[position],""+position);
                    transaction.show(fragment[position]);
                }
                transaction.commit();
            }
        }

    }

}
